// ============================================
// internal/config/config.go
// ============================================

package config

import (
	"os"

	"gopkg.in/yaml.v3"
)

type Config struct {
	Server struct {
		Host string `yaml:"host"`
		Port int    `yaml:"port"`
	} `yaml:"server"`

	Libvirt struct {
		AutoConnect bool   `yaml:"auto_connect"`
		DefaultURI  string `yaml:"default_uri"`
	} `yaml:"libvirt"`

	Logging struct {
		Level  string `yaml:"level"`
		Format string `yaml:"format"`
	} `yaml:"logging"`
}

func Load(filename string) (*Config, error) {
	data, err := os.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	var cfg Config
	if err := yaml.Unmarshal(data, &cfg); err != nil {
		return nil, err
	}

	return &cfg, nil
}

func Default() *Config {
	cfg := &Config{}
	cfg.Server.Host = "0.0.0.0"
	cfg.Server.Port = 8080
	cfg.Libvirt.AutoConnect = true
	cfg.Libvirt.DefaultURI = "qemu:///system"
	cfg.Logging.Level = "info"
	cfg.Logging.Format = "json"
	return cfg
}
